<template>
    <div>
        <el-row justify="center" type="flex">
            <el-col :span="20" style="margin-top: 75px">
                <el-form ref="form" :model="form" label-width="200px">
                    <h3>自动调度开启</h3>
                    <el-form-item label="爬虫自动调度">
                        <el-switch v-model="form.scrapyStatus" @change="changeScrapyStatus"></el-switch>
                    </el-form-item>
                    <el-form-item label="Spark分析自动调度">
                        <el-switch v-model="form.sparkStatus" @change="changeSparkStatus"></el-switch>
                    </el-form-item>
                    <h3>立即调度</h3>
                    <el-form-item>
                        <el-button type="warning" @click="startScrapy">立即爬虫</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="warning" @click="startSpark">立即分析</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    export default {
        name: "scheduler",
        data() {
            return {
                form: {
                    delivery: '',
                    sparkStatus:true,
                    scrapyStatus:true,
                },
            }
        },
        methods: {
            submit() {
                console.log(this.form.delivery)
            },
            setDefaultValue() {
                this.$http.get('/scheduler/spark/status')
                    .then(form => {
                        this.form.sparkStatus = form.data['bool'];
                    }).catch(err =>this.$message.warning("无权访问调度器状态"))
                this.$http.get('/scheduler/scrapy/status')
                    .then(form => {
                        this.form.scrapyStatus = form.data['bool'];
                    }).catch(err => this.$message.warning("无权访问调度器状态"));
            },
            changeSparkStatus() {
                this.$http({
                    url: this.$http.adornUrl('/scheduler/spark/change'),
                    method: 'get',
                    params: {
                        'bool': this.form.sparkStatus,
                    },
                });
            },
            changeScrapyStatus() {
                this.$http({
                    url: this.$http.adornUrl('/scheduler/scrapy/change'),
                    method: 'get',
                    params: {
                        'bool': this.form.scrapyStatus,
                    },
                });
            },
            startSpark() {
                this.$http.get('/scheduler/spark/startImmediately')
                    .then(form=>{
                        this.$message.success("进行了一次Spark调度");
                    }).catch(err=>{
                        this.$message.error('开启失败，请检查原因')
                });
            },
            startScrapy() {
                this.$http.get('/scheduler/scrapy/startImmediately')
                    .then(form=>{
                        this.$message.success("进行了一次Scrapy调度");
                    }).catch(err=>{
                    this.$message.error('开启失败，请检查原因')
                });
            },
        },
        mounted() {
            this.setDefaultValue()
        },
    }
</script>

<style scoped>

</style>